package com.service.impl;

import com.bean.entity.Village;
import com.bean.pojo.PageAssistant;
import com.dao.VillageMapper;
import com.service.VillageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class VillageServiceImpl implements VillageService {
    @Autowired
    private VillageMapper villageMapper;
    @Override
    public int save(Village t) throws Exception {
        int insert = villageMapper.insert(t);
        if (insert==1) return 1;
        throw new Exception("小区信息新增失败！");
    }

    @Override
    public int remove(Village t) throws Exception {
        int delete = villageMapper.delete(t);
        if (delete==1) return 1;
        throw new Exception("小区信息删除失败！");
    }

    @Override
    public int modify(Village t) throws Exception {
        int update = villageMapper.update(t);
        if (update==1) return 1;
        throw new Exception("小区信息修改失败！");
    }

    @Override
    public Village unique(Village t) throws Exception {
        List<Village> villageList = villageMapper.select(t);
        if (villageList.size()!=1) throw new Exception("没有相关的小区信息");
        return villageList.get(0);
    }

    @Override
    public List<Village> query(Village t) {
        List<Village> villageList = villageMapper.select(t);
        return villageList;
    }

    @Override
    public PageAssistant<Village> queryByPage(Map<String, Object> map, PageAssistant<Village> pa) {

        //查询并设置总记录数
        pa.setCount(villageMapper.selectCount(map));

        //添加分页参数
        map.put("startCode",pa.getStartCode());
        map.put("pageSize",pa.getPageSize());

        //查询并设置分页内容
        pa.setItems(villageMapper.selectByPage(map));
        return pa;
    }
}
